package com.biz.primus.ms.promotion.dao.po;

import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.model.promotion.enums.PromotionGiftTypeEnum;
import com.biz.primus.model.promotion.enums.PromotionSignatureRuleTypeEnum;
import com.biz.primus.model.promotion.enums.PromotionSignatureTypeEnum;
import com.biz.primus.model.promotion.vo.OrderPromotionDetailVo;
import com.biz.primus.model.promotion.vo.SignatureGiftRuleVo;
import com.biz.primus.model.promotion.vo.req.OrderPromotionReqDetailVo;
import com.biz.primus.model.promotion.vo.req.TakeMemberSignaturePromotionReqVo;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * 签到/评价有礼促销
 *
 * @author david-liu
 */
@Entity
@Getter
@Setter
public class SignaturePromotion extends BasePromotion implements Comparable<SignaturePromotion> {
    private static final long serialVersionUID = -1544556147624500248L;

    // 促销方式(按照签到还是按照评价)
    @Enumerated(value = EnumType.STRING)
    private PromotionSignatureTypeEnum signatureType;

    // 签到次数限制(针对按照签到次数做促销)
    @Enumerated(value = EnumType.STRING)
    private PromotionSignatureRuleTypeEnum signatureRuleType;

    /**
     * 需要连续签到/评价多少次促销才能生效
     */
    private Integer effectTimes;

    /**
     * 奖品类型
     */
    @Enumerated(value = EnumType.STRING)
    private PromotionGiftTypeEnum giftType;

    /**
     * 签到有礼规则Vo, 存放SignatureGiftRuleVo JSON
     *
     * @see SignatureGiftRuleVo
     */
    private String signatureGiftRule;

    @Override
    public boolean isMatchPromotionRule(OrderPromotionReqDetailVo detailVo) {
        return false;
    }

    @Override
    public OrderPromotionDetailVo getOrderPromotionDetail(OrderPromotionReqDetailVo reqDetailVo) {
        return null;
    }

    public boolean isMatchPromotionRule(TakeMemberSignaturePromotionReqVo reqVo) {
        if (reqVo.getSignatureType() == PromotionSignatureTypeEnum.BY_SIGN_IN) {
            if (this.signatureRuleType == PromotionSignatureRuleTypeEnum.BY_COMBO_SIGN_IN_TIMES) {
                return effectTimes <= reqVo.getComboSignInTimes();
            }
            if (this.signatureRuleType == PromotionSignatureRuleTypeEnum.BY_SIGN_IN_TIMES) {
                return effectTimes <= reqVo.getTotalSignInTimes();
            }
        }

        return reqVo.getSignatureType() == PromotionSignatureTypeEnum.BY_EVALUATION && effectTimes <= reqVo.getTotalGoodEvaluationTimes();
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
    }

    @Override
    public int compareTo(SignaturePromotion o) {
        if (this.startTime == null) {
            if (o.startTime == null) {
                return 0;
            } else {
                return -1;
            }
        } else {
            if (o.startTime == null) {
                return 1;
            } else {
                return this.startTime.compareTo(o.startTime);
            }
        }
    }

    public SignatureGiftRuleVo getSignatureGiftRuleVo() {
        if (StringUtils.isBlank(this.signatureGiftRule)) {
            return null;
        } else {
            return JsonUtils.json2Obj(this.signatureGiftRule, SignatureGiftRuleVo.class);
        }
    }
}
